﻿using BCCommon;
using BCDto.Common.News.NewsContent;
using BCService.Common.News.NewsContentService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace BCTMSRESTful.Controllers.WebDomain
{
    /// <summary>
    /// 新闻控制器（开放）
    /// </summary>
    [Route("api/v1/[controller]")]
    [ApiController]
    public class NewsContentController : BCTMSRESTfulAPIController
    {
        private readonly INewsContentService newsContentService;

        public NewsContentController(INewsContentService newsContentService)
        {
            this.newsContentService = newsContentService;
        }

        /// <summary>
        /// 获取新闻内容列表（分页）
        /// </summary>
        /// <param name="contentCategoryId">分类名称</param>
        /// <param name="index">当前页</param>
        /// <param name="size">每页条数</param>
        /// <response code="200">带有分页信息的新闻内容列表。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<NewsContentViewDto>), 200)]
        [AllowAnonymous]
        public IActionResult Get([FromQuery(Name = "content_category_id")] int? contentCategoryId,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return this.Ok(this.newsContentService.GetViewList(contentCategoryId, null, null, false,false, index, size));
        }

        /// <summary>
        /// 用户通知列表（分页）
        /// </summary>       
        /// <param name="index">当前页</param>
        /// <param name="size">每页条数</param>
        /// <response code="200">带有分页信息的通知信息列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<NewsContentViewDto>), 200)]
        [AllowAnonymous]
        public IActionResult UserNotification(
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return this.Ok(this.newsContentService.GetViewList(12, null, null,  false, false, index, size));
        }

        /// <summary>
        /// 司机通知列表（分页）
        /// </summary>       
        /// <param name="index">当前页</param>
        /// <param name="size">每页条数</param>
        /// <response code="200">带有分页信息的通知信息列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<NewsContentViewDto>), 200)]
        [AllowAnonymous]
        public IActionResult DriverNotification(
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return this.Ok(this.newsContentService.GetViewList(13, null, null,  false, false, index, size));
        }

        /// <summary>
        /// 获取一条新闻内容
        /// </summary>
        /// <param name="newsContentId">新闻内容ID</param>
        /// <returns></returns>
        [HttpGet("{news_content_id}")]
        [ProducesResponseType(typeof(NewsContentDto), 200)]
        public IActionResult Get([FromRoute(Name = "news_content_id")]long newsContentId)
        {
            return this.Ok(this.newsContentService.GetNewsContentById(newsContentId, true));
        }

        /// <summary>
        /// 获取内容简要列表
        /// </summary>
        /// <param name="contentCategoryId">分类名称</param>
        /// <param name="startIndex">起始下标</param>
        /// <param name="amount">数量</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<NewsContentDto>), 200)]
        public IActionResult GetContentList(
            [FromQuery(Name = "content_category_id")]int contentCategoryId,
            [FromQuery(Name = "start_index")]int startIndex = 0,
            [FromQuery(Name = "amount")]int amount = 2)
        {
            return this.Ok(this.newsContentService.GetContentListByIndexAndAmount(contentCategoryId, startIndex, amount));
        }

        /// <summary>
        /// 获取新闻内容
        /// </summary>
        /// <param name="newsContentId">新闻ID</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<NewsContentDto>), 200)]
        public IActionResult GetNewById([FromQuery(Name ="news_content_id")]long newsContentId)
        {
            return this.Ok(this.newsContentService.GetNewById(newsContentId));
        }

        /// <summary>
        /// 更新转载次数
        /// </summary>
        /// <param name="newsContentId">新闻内容ID</param>
        /// <returns></returns>
        [HttpPut("{news_content_id}")]
        [ProducesResponseType(typeof(NewsContentDto), 200)]
        public IActionResult UpdateReprintCount([FromRoute(Name = "news_content_id")]int newsContentId)
        {
            return this.Ok(this.newsContentService.UpdateReprintCount(newsContentId));
        }

        /// <summary>
        /// 根据关键字查询新闻
        /// </summary>
        /// <param name="contentCategoryId">分类ID</param>
        /// <param name="keyWord">关键字</param>
        /// <param name="pageSize">每页数据条数</param>
        /// <param name="pageIndex">当前页码</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<NewsContentDto>), 200)]
        public IActionResult QueryNewsByKeyWord(
            [FromQuery(Name = "content_category_id")]int? contentCategoryId,
            [FromQuery(Name = "key_word")]string keyWord,
            [FromQuery(Name = "index")]int pageIndex = 1,
            [FromQuery(Name = "size")]int pageSize = 10)
        {
            return this.Ok(this.newsContentService.QueryNewsByKeyWord(contentCategoryId, keyWord, pageSize, pageIndex));
        }

        /// <summary>
        /// 查询上一篇或下一篇
        /// </summary>
        /// <param name="newsContentId">新闻内容ID</param>
        /// <param name="isNext">是否下一页</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(NewsContentDto), 200)]
        public IActionResult QueryPreviouOrNext(
            [FromQuery(Name = "news_content_id")]long newsContentId,
            [FromQuery(Name = "is_next")]bool isNext)
        {
            return this.Ok(this.newsContentService.QueryPreviouOrNext(newsContentId,isNext));
        }
    }
}
